<template>
  <el-main>
    <!-- 面包屑头顶标签 -->
    <el-header>
      <h4>条形码发放记录信息管理</h4>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>条形码发放记录信息管理 </el-breadcrumb-item>
        <el-breadcrumb-item>条形码发放记录信息列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-header>
    <!-- 头顶标签结束 -->
    <!-- 页面信息 -->
    <el-card>
      <!-- 搜索与添加 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-button type="primary" @click="addDialogVisible = true">
            <span class="el-icon-circle-plus-outline"></span
            >添加条形码发放记录信息</el-button
          >
          <el-button
            type="success"
            icon="el-icon-refresh"
            @click="getList()"
          ></el-button>
        </el-col>
        <el-col :span="8" :offset="8">
          <div>
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              @keydown="getList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getList"
              ></el-button>
            </el-input></div
        ></el-col>
      </el-row>
      <!-- 信息表格 -->
      <el-table :data="tableData" border>
        <el-table-column
          label="发放档口名"
          prop="stallsName"
          sortable
        ></el-table-column>
        <el-table-column
          label="发放时间"
          prop="grantdate"
          sortable
          :formatter="dateFormat"
        ></el-table-column>
        <el-table-column label="发放数量" prop="grantcount" sortable>
        </el-table-column>
        <el-table-column
          label="开始编号"
          prop="startnumber"
          sortable
        ></el-table-column>
        <el-table-column
          label="结束编号"
          prop="endnumber"
          sortable
        ></el-table-column>
        <el-table-column
          label="发放人员"
          prop="deliverName"
          sortable
        ></el-table-column>
        <el-table-column
          label="领取人员"
          prop="receiveName"
          sortable
        ></el-table-column>
        <el-table-column label="操作" width="160px">
          <!-- eslint-disable-next-line -->
          <template slot-scope="scope" style="margin-right: 0px">
            <el-button
              type="success"
              class="el-icon-setting"
              size="mini"
              @click="showDetialDialogVisible(scope.row.id)"
              >明细</el-button
            >
            <el-button
              type="primary"
              class="el-icon-edit"
              size="mini"
              @click="showEditDialogVisible(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              class="el-icon-delete"
              size="mini"
              @click="deleteList(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 25, 50, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 信息表格结束 -->
    </el-card>
    <!-- 编辑弹窗信息页面 -->
    <el-dialog
      title="编辑信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="dialogClose()"
    >
      <!-- 内容主题区 -->
      <el-card>
        <el-form
          :model="Form"
          :rules="FormRules"
          ref="FormRef"
          label-width="100px"
          :disabled="true"
        >
          <el-form-item label="条形码编号" prop="barcodeid">
            <el-input v-model="Form.barcodeid"></el-input>
          </el-form-item>
          <el-form-item label="发放档口名" prop="stallsName">
            <el-input v-model="Form.stallsName"></el-input>
          </el-form-item>
          <el-form-item label="发放时间" prop="grantdate">
            <el-date-picker
              v-model="Form.grantdate"
              align="right"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
              <!-- <el-input v-model="Form.loginDate" ></el-input> -->
            </el-date-picker>
          </el-form-item>
          <el-form-item label="发放数量" prop="grantcount">
            <el-input v-model="Form.grantcount"></el-input>
          </el-form-item>
          <el-form-item label="开始编号" prop="startnumber">
            <el-input v-model="Form.startnumber"></el-input>
          </el-form-item>
          <el-form-item label="结束编号" prop="endnumber">
            <el-input v-model="Form.endnumber"></el-input>
          </el-form-item>
          <el-form-item label="发放人员" prop="deliverName">
            <el-select
              v-model="Form.deliverName"
              filterable
              placeholder="请选择"
            >
            </el-select>
          </el-form-item>
          <el-form-item label="领取人员" prop="receiveName">
            <el-select
              v-model="Form.receiveName"
              filterable
              placeholder="请选择"
            >
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose()">取 消</el-button>
        <el-button type="primary" @click="editMsgForm()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 页面信息结束 -->
    <!-- 明细弹窗信息页面 -->
    <el-dialog
      title="编辑信息"
      :visible.sync="detialDialogVisible"
      width="50%"
      @close="dialogClose()"
    >
      <!-- 内容主题区 -->
      <el-card>
        <el-form
          :model="Form"
          :rules="FormRules"
          ref="FormRef"
          label-width="100px"
        >
          <el-form-item label="脚标机编号" prop="barcodeMachineId">
            <el-input v-model="Form.barcodeMachineId"></el-input>
          </el-form-item>
          <el-form-item label="登陆时间" prop="loginDate">
            <el-date-picker
              v-model="Form.loginDate"
              align="right"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
              <!-- <el-input v-model="Form.loginDate" ></el-input> -->
            </el-date-picker>
          </el-form-item>
          <el-form-item label="在线状态" prop="state">
            <el-input v-model="Form.state"></el-input>
          </el-form-item>
          <el-form-item label="条形码剩余" prop="barcodeSurplus">
            <el-input v-model="Form.barcodeSurplus"></el-input>
          </el-form-item>
          <el-form-item label="用户" prop="name">
            <el-select v-model="Form.name" filterable placeholder="请选择">
              <el-option
                v-for="item in userName"
                :key="item.userId"
                :label="item.loginName + `[id=` + item.userId + `]`"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="下线时间" prop="offlineTime">
            <el-date-picker
              v-model="Form.offlineTime"
              align="right"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
              <!-- <el-input v-model="Form.loginDate" ></el-input> -->
            </el-date-picker>
          </el-form-item>
          <el-form-item label="创建者" prop="loginName">
            <el-input v-model="Form.loginName" :disabled="true"></el-input>
          </el-form-item>
        </el-form>
      </el-card>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose()">取 消</el-button>
        <el-button type="primary" @click="dialogClose()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 页面信息结束 -->
  </el-main>
</template>

<script>
export default {
  name: "barcodeGrantList",
  data() {
    return {
      queryInfo: {
        //模糊查找输入
        query: "",
        //初始化当前页数
        pageNum: 1,
        //初始化当前每页显示多少条
        pageSize: 50,
      },
      //用户列表数据
      userName: [],
      //表格数据
      tableData: [],
      total: 0,
      size: 0,
      //控制对话框弹出
      editDialogVisible: false,
      detialDialogVisible: false,
      //信息的表单数据
      Form: {
        // 有bug
        barcodeid: "",
        stallsName: "",
        grantdate: "",
        grantcount: "",
        startnumber: "",
        endnumber: "",
        deliverName: "",
        receiveName:"",
        stallsMessageVoPage: [],
      },
      //验证规则
      FormRules: {
        barcodeMachineId: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        stallsName: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        grantdate: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        grantcount: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        startnumber: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        endnumber: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        deliverName: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        receiveName: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //模糊查询
    async searchList() {
      this.queryInfo.pageNum = 1;
      this.queryInfo.pageSize = 50;
      if (this.queryInfo.query == "") {
        this.getList();
        return;
      }
      const { data: res } = await this.reqM1Service(
        `barcodeMachineStatus/barcodeMachineStatus/findByLike?input=${this.queryInfo.query}&pageCurrent=${this.queryInfo.pageNum}&pageSize=${this.queryInfo.pageSize}`,
        "",
        "get"
      );
      console.log(res);
      if (res.code != 2000) {
        return this.$message.error(res.message);
      }
      this.tableData = res.data.records;
      this.total = res.data.total;
    },
    //获取信息
    async getList() {
      const { data: res } = await this.reqM1Service(
        `labelAndBarcode/barcode-grant/findAllOnPagination?pageCurrent=${this.queryInfo.pageNum}&pageSize=${this.queryInfo.pageSize}`,
        "",
        "get"
      );
      console.log(res);
      if (res.code != 2000) {
        return this.$message.error(res.message);
      }
      this.tableData = res.data.records;
      this.total = res.data.total;
    },
    //监听  页码值  改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getList();
    },
    //监听 当前页数 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getList();
    },
    //删除
    async deleteList(id) {
      const { data: res } = await this.reqM1Service(
        `labelAndBarcode/barcode-grant/deleteBarcodeGrant/${id}`,
        "",
        "delete"
      );
      console.log(res);
      if (res.code == 2000) {
        this.getList();
        return this.$message.success(res.message);
      } else {
        return this.$message.error(res.message);
      }
    },
    //对话框关闭重置内容
    dialogClose() {
      this.$refs.FormRef.resetFields();
      this.editDialogVisible = false;
      this.detialDialogVisible = false;
    },
    //展示明细弹窗
    showDetialDialogVisible(id) {
      this.detialDialogVisible = true;
      this.showData(id);
      this.getUserName();
    },
    //展示编辑弹窗
    showEditDialogVisible(id) {
      this.editDialogVisible = true;
      this.showData(id);
      this.getUserName();
    },
    //渲染原有数据
    async showData(id) {
      const { data: res } = await this.reqM1Service(
        `labelAndBarcode/barcode-grant/findBarcodeGrantVoById/${id}`,
        "",
        "get"
      );
      console.log(res);
      if (res.code !== 2000) {
        return this.$message.error(res.message);
      } else {
        this.Form = res.data;
      }
    },
    //编辑表单信息提交
    async editMsgForm() {
      //表单预验证
      this.$refs.FormRef.validate(async (validate) => {
        console.log(validate);
        if (!validate) return;
        //发起请求
        await this.reqM1Service(
          `labelAndBarcode/barcode-grant/updateBarcodeGrant`,
          this.Form,
          "put"
        ).then(({ data: res }) => {
          console.log(res);
          if (res.code != 2000) {
            return this.$message.error("编辑失败！");
          } else {
            this.$message.success("编辑成功！");
            this.dialogClose();
            this.getList();
          }
        });
      });
    },
    //用户列表获取
    async getUserName() {
      await this.reqM1Service(
        `barcodeMachineStatus/barcodeMachineStatus/indUserNameVoList`,
        "",
        "get"
      ).then(({ data: res }) => {
        console.log(res);
        if (res.code != 2000) {
          return this.$message.error("获取用户失败！");
        } else {
          this.userName = res.data;
          console.log(this.userName);
        }
      });
    },

    //时间格式化
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property];
      if (daterc != null) {
        const dateMat = new Date(
          parseInt(daterc.replace("/Date(", "").replace(")/", ""), 10)
        );
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const hh = dateMat.getHours();
        const mm = dateMat.getMinutes();
        const ss = dateMat.getSeconds();
        const timeFormat =
          year + "/" + month + "/" + day + " " + hh + ":" + mm + ":" + ss;
        return timeFormat;
      }
    },
  },
  created() {
    this.getList();
  },
};
</script>
<style lang="less" scoped>
/deep/ th {
  color: #606266;
}
/deep/ .el-button + .el-button {
  margin-left: 5px;
}
/deep/ .el-button--mini {
  padding: 6px 2px;
}
</style>
